package cn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.entity.usualassessment.UsAssTemplateIndexEntity;
import cn.kpi.entity.vtiondpline.VtDpUserListEntity;
import cn.kpi.service.sys.IOrgService;
import cn.kpi.service.sys.IUserService;
import cn.kpi.service.usualassessment.IUsAssTemplateService;
import cn.kpi.service.vtiondpline.IVtDpUserService;

@Controller
@RequestMapping("common")
public class CommonController extends BaseController {
	@Autowired
	private IUserService iUserService;
	
	@Autowired
	private IOrgService  iOrgService;
	
	@Autowired 
	private IVtDpUserService iVtDpUserService;
	
	@Autowired
	private IUsAssTemplateService iUsAssTemplateService;


	@ResponseBody
	@RequestMapping(value = "/get_audiouser_list")
	public List<UserEntity> GetAudioUserList(HttpServletRequest request) {
		try {
//			String whereuser=this.GetUserListByRole();
//
			String whereuser = "";
			String key = request.getParameter("key");
			if (key != null && key.length() > 0) {
				whereuser += " AND ( keyword LIKE '%" + key + "%' OR realname LIKE '%" + key + "%')";
			}
			whereuser += "and ldzw!=''";
			List<UserEntity> userlst = iUserService.GetUserListByWhere(whereuser);
			for (int i = 0; i < userlst.size(); i++) {
				UserEntity entity = userlst.get(i);
				userlst.get(i).setRealname(entity.getRealname() + "(" + entity.getCardno() + ")");
			}
			return userlst;
		} catch (Exception ex) {
			return null;
		}
	}

	@ResponseBody
	@RequestMapping(value = "/get_Leader_list")
	public List<UserEntity> GetLeaderList(HttpServletRequest request) {
		try {
//			String whereuser=this.GetUserListByRole();
//
			String whereuser = "";
			String key = request.getParameter("key");
			if (key != null && key.length() > 0) {
				whereuser += " AND ( keyword LIKE '%" + key + "%' OR realname LIKE '%" + key + "%')";
			}
			whereuser += "and rank=5";
			List<UserEntity> userlst = iUserService.GetUserListByWhere(whereuser);
			for (int i = 0; i < userlst.size(); i++) {
				UserEntity entity = userlst.get(i);
				userlst.get(i).setRealname(entity.getRealname() + "(" + entity.getCardno() + ")");
			}
			return userlst;
		} catch (Exception ex) {
			return null;
		}
	}



	@ResponseBody
	@RequestMapping(value="/get_user_list")
	public List<UserEntity> GetUserList(HttpServletRequest request){
		try{
			String whereuser=this.GetUserListByRole();
			
			//String whereuser="";
			String key = request.getParameter("key");
			if(key!=null&&key.length()>0){
				whereuser+=" AND ( keyword LIKE '%"+key+"%' OR realname LIKE '%"+key+"%')";
			}
			List<UserEntity> userlst = iUserService.GetUserListByWhere(whereuser);
			for(int i=0;i<userlst.size();i++){
				UserEntity entity=userlst.get(i);
				userlst.get(i).setRealname(entity.getRealname()+"("+entity.getCardno()+")");
			}
			return userlst;
		}
		catch(Exception ex){
			return null;
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/get_user_list_temp")
	public List<UserEntity> GetUserListTemp(HttpServletRequest request){
		try{
			String whereuser=this.GetUserListByRole();
			
			//String whereuser="";
			String key = request.getParameter("key");
			if(key!=null&&key.length()>0){
				whereuser+=" AND ( keyword LIKE '%"+key+"%' OR realname LIKE '%"+key+"%')";
			}
			whereuser+=" AND userid IN (SELECT userid FROM t_user_usasstemp WHERE status=1)";
			List<UserEntity> userlst = iUserService.GetUserListByWhere(whereuser);
			for(int i=0;i<userlst.size();i++){
				UserEntity entity=userlst.get(i);
				userlst.get(i).setRealname(entity.getRealname()+"("+entity.getCardno()+")");
			}
			return userlst;
		}
		catch(Exception ex){
			return null;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/get_org_list")
	public List<OrgEntity> GetOrgList(int orgtype) {
		List<OrgEntity> lst=null;
		try
		{
			String where="";
			if(orgtype==0){
				where+=" AND orgtype=0 ";
				where+=" AND orgid IN("+this.GetOrgListByRole()+") ";
			}
			else if(orgtype==1){
				where+=" AND orgtype IN (0,1) ";
				where+=" AND (orgid IN("+this.GetOrgListByRole()+") OR orgparentid IN("+this.GetOrgListByRole()+"))";
			}
			
			lst = iOrgService.GetOrgListByWhere(where);
			return lst;
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/get_org_list_jd")
	public List<OrgEntity> GetOrgListJd(int orgtype) {
		List<OrgEntity> lst=null;
		try
		{
			String where="";
			if(orgtype==0){
				where+=" AND orgtype=0 ";
			}
			else if(orgtype==1){
				where+=" AND orgtype IN (0,1) ";
			}
			//where+=" AND orgid!="+this.GetLoginUser().getSzdw();
			lst = iOrgService.GetOrgListByWhere(where);
			return lst;
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/get_org_list_byorgid")
	public List<OrgEntity> GetOrgListByOrgid(Long orgid) {
		List<OrgEntity> lst=null;
		try
		{
			String where=" AND orgparentid="+orgid;
			
			lst = iOrgService.GetOrgListByWhere(where);
			return lst;
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/get_index_list")
	public List<UsAssTemplateIndexEntity> GetIndexListByOrgid(Long groupid) {
		List<UsAssTemplateIndexEntity> lst=null;
		try
		{
			String where = " AND assindextype=0 AND groupid="+groupid;
			if(this.GetLoginUserId()==0){
				where+=" AND byuserid=0";
			}
			else{
				String orgids=this.GetOrgListByRole();
				where+=" AND byuserid IN (SELECT userid FROM t_sys_user WHERE szdw IN ("+orgids+"))";
			}
			
			lst = iUsAssTemplateService.GetTemplateIndexListByWhere(where);
			return lst;
		}
		catch(Exception ex){
			ex.printStackTrace();
			return null;
		}
	}
	
	@RequestMapping(value = "/user_vtdp")
	public String UserVtdp(Model model,Long userid,int year,int quarter) {
		try{
			String where="";
			
//			switch(quarter){
//			case 0:
//				where+=" AND begintime>='"+year+"-01-01 00:00:00' AND begintime<='"+year+"-12-31 23:59:59'";
//				break;
//			case 1:
//				where+=" AND begintime>='"+year+"-01-01 00:00:00' AND begintime<='"+year+"-03-31 23:59:59'";
//				break;
//			case 2:
//				where+=" AND begintime>='"+year+"-04-01 00:00:00' AND begintime<='"+year+"-06-30 23:59:59'";
//				break;
//			case 3:
//				where+=" AND begintime>='"+year+"-07-01 00:00:00' AND begintime<='"+year+"-09-30 23:59:59'";
//				break;
//			case 4:
//				where+=" AND begintime>='"+year+"-10-01 00:00:00' AND begintime<='"+year+"-12-31 23:59:59'";
//				break;
//			}
			
			
			switch(quarter){
			case 0://quarter = 0 默认为年
				where+="AND begintime<='"+year+"-12-31 23:59:59' AND begintime>='"+year+"-01-01 00:00:00'";
				break;
			case 1://第一季
				//vtdpwhere+=" AND begintime>='"+year+"-01-01 00:00:00' AND begintime<='"+year+"-03-31 23:59:59'";
				where+="AND (( begintime<='"+year+"-01-01 00:00:00' AND endtime>='"+year+"-01-01 00:00:00') OR ( begintime<='"+year+"-03-31 23:59:59' AND endtime>='"+year+"-03-31 23:59:59')) ";
				break;
			case 2://第二季
				//vtdpwhere+=" AND begintime>='"+year+"-04-01 00:00:00' AND begintime<='"+year+"-06-30 23:59:59'";
				where+=" AND (( begintime<='"+year+"-04-01 00:00:00' AND endtime>='"+year+"-04-01 00:00:00') OR ( begintime<='"+year+"-06-30 23:59:59' AND endtime>='"+year+"-06-30 23:59:59')) ";
				break;
			case 3://第三季
				//vtdpwhere+=" AND begintime>='"+year+"-07-01 00:00:00' AND begintime<='"+year+"-09-30 23:59:59'";
				where+="AND (( begintime<='"+year+"-07-01 00:00:00' AND endtime>='"+year+"-07-01 00:00:00') OR ( begintime<='"+year+"-09-30 23:59:59' AND endtime>='"+year+"-09-30 23:59:59')) ";
				break;
			case 4://第四季
				//vtdpwhere+=" AND begintime>='"+year+"-10-01 00:00:00' AND begintime<='"+year+"-12-31 23:59:59'";
				where+="AND (( begintime<='"+year+"-10-01 00:00:00' AND endtime>='"+year+"-10-01 00:00:00') OR ( begintime<='"+year+"-12-31 23:59:59' AND endtime>='"+year+"-12-31 23:59:59')) ";
				break;
			}
			
			List<VtDpUserListEntity> lst=iVtDpUserService.GetVtDpListByUseridAndWhere(userid,where);
			model.addAttribute("lst", lst);
		}
		catch(Exception ex){
			
		}
		return "common/user_vtdp";
	}
}
